from dotenv import load_dotenv

load_dotenv()

import requests

# 要发送的数据
data = {
    "question": "查询所有的设备信息",
    "sql": """
     SELECT t1.id,
		t1.name,
		t1.code AS device_code,
		t1.organ_id,
		t1.lamp_post_id,
		t1.online_state,
		NULL::text AS startup_state,
		'device_camera'::text AS device_type
	FROM device.device_camera t1
	UNION
	SELECT t2.id,
		t2.name,
		t2.code AS device_code,
		t2.organ_id,
		t2.lamp_post_id,
		t2.online_state,
		NULL::text AS startup_state,
		'device_env'::text AS device_type
	FROM device.device_env t2
	UNION
	SELECT t3.id,
		t3.name,
		t3.code AS device_code,
		t3.organ_id,
		t3.lamp_post_id,
		t3.online_state,
		NULL::text AS startup_state,
		'device_broadcast'::text AS device_type
	FROM device.device_broadcast t3
	UNION
	SELECT t4.id,
		t4.name,
		t4.code AS device_code,
		t4.organ_id,
		t4.lamp_post_id,
		t4.online_state,
		NULL::text AS startup_state,
		'device_edge_gateway'::text AS device_type
	FROM device.device_edge_gateway t4
	UNION
	SELECT t5.id,
		t5.name,
		t5.code AS device_code,
		t5.organ_id,
		t5.lamp_post_id,
			CASE
				WHEN terminal.isonline::text = '0'::text THEN 'offline'::text
				WHEN terminal.isonline::text = '1'::text THEN 'online'::text
				ELSE NULL::text
			END AS online_state,
			CASE
				WHEN terminal.status::text = '0'::text THEN 'off'::text
				WHEN terminal.status::text = '1'::text THEN 'on'::text
				ELSE NULL::text
			END AS startup_state,
		'device_screen'::text AS device_type
	FROM device.device_screen t5,
		mediamanager.terminal
	WHERE t5.code::text = terminal.code::text
	UNION
	SELECT t6.id,
		t6.name,
		t6.code AS device_code,
		t6.organ_id,
		t6.lamp_post_id,
		t6.online_state,
		NULL::text AS startup_state,
		'device_talk'::text AS device_type
	FROM device.device_talk t6
	UNION
	SELECT t7.id,
		t7.name,
		t7.mac AS device_code,
		t7.organ_id,
		t7.lamp_post_id,
		t7.online_state,
		NULL::text AS startup_state,
		'device_wireless'::text AS device_type
	FROM device.device_wireless t7
	UNION
	SELECT l_alonelamp.alone_lamp_id AS id,
		l_alonelamp.alone_lamp_name AS name,
		l_alonelamp.alone_lamp_code AS device_code,
		l_lamppost.organ_id,
		l_alonelamp.lamp_post_id,
			CASE
				WHEN l_coplog.is_online = 1 THEN 'online'::text
				ELSE 'offline'::text
			END AS online_state,
			CASE
				WHEN l_coplog.is_switch_on > 0 THEN 'on'::text
				ELSE 'off'::text
			END AS startup_state,
		'device_alonelamp'::text AS device_type
	FROM device.l_alonelamp
		LEFT JOIN device.l_lamppost ON l_alonelamp.lamp_post_id::text = l_lamppost.lamp_post_id::text
		LEFT JOIN light.l_coplog ON l_alonelamp.alone_lamp_id::text = l_coplog.alone_lamp_id::text
	UNION
	SELECT l_alonelamp_control.alone_lamp_control_id AS id,
		NULL::text AS name,
		l_alonelamp_control.product_number AS device_code,
		l_lamppost.organ_id,
		l_alonelamp_control.lamp_post_id,
		NULL::text AS online_state,
		NULL::text AS startup_state,
		'device_alonelamp_control'::text AS device_type
	FROM device.l_alonelamp_control
		LEFT JOIN device.l_lamppost ON l_alonelamp_control.lamp_post_id::text = l_lamppost.lamp_post_id::text
     
     """
    #"ddl": """ """
}

# 请求的URL
url = "http://127.0.0.1:5000/api/v0/train"

# 发送POST请求
response = requests.post(url, json=data)

# 检查响应状态码
if response.status_code == 200:
    # 打印响应内容
    print("Response:")
    print(response.text)
else:
    print("Error:", response.status_code)
